/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package spelet;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.List;



public class Hero extends Entity {
    //värde mellan 0-3, anger spelarens springhastighet
private int runSpeed=0;
    //antalet frames som gubben varit springandes
private int runTime=0;
    //antalet frames som gubben bromsat
private int stopTime=0;
    //Hastighet i luften
private int airSpeed=0;
    //Riktning som gubben tittar åt +1=höger -1=vänster
private int airTime=0;
private int direction=0;
private boolean canJump;
private boolean running=false;
private Sound sound=new Sound();
String jump="jump.wav";
String walk="run.wav";

    Hero(int x, int y) {
        s = new Sprite(Toolkit.getDefaultToolkit().getImage("Sprites\\hero.gif"));
        position_x = x;
        position_y = y;
        type="hero";
        sound.Load(jump);
        sound.Load(walk);

    }

    public void draw(Graphics g) {
        s.draw(g,position_x,position_y);
    }


    public void moveX(int x) {
        position_x=position_x+x;

    }


    public void moveY(int y) {
        position_y=position_y+ y;

    }
    // Metod som tar hand om gubbens springande, sköter acceleration,bromsning,
    //riktining osv. Det som måste anger är vilken rikning gubben ska springa åt
    public void run(List<Entity> entities,int dir){
        //Springer genom acceleration tills spelaren uppnått maxhastighet
        if(running==false && dir!=0){
            sound.Loop(walk);
            running=true;
            System.out.println("lol");
        }
        else if(running==true && dir==0){
            sound.Stop(walk);
            running=false;
        }

        if (dir != 0) {
            direction=dir;
            runTime++;

            if (runTime < 10) {
                runSpeed = dir * 4;
            } else if (runTime > 10 && runTime<=15) {
                runSpeed = dir * 5;
            }
            else if (runTime > 15 && runTime<=20) {
                runSpeed = dir * 6;
                stopTime=4;
            }
            else if (runTime > 20) {
                runSpeed = dir * 8;
                stopTime=8;
            }

            if((runTime > 100)){
                stopTime=10;
            }
            

                moveX(runSpeed);
            
                
            }
        
        //Bromsar då gubben byter riktining eller slutar springa
        else if(dir==0 && stopTime!=0){
            
            stopTime--;
            runTime=0;


            if(stopTime>=10){
                runSpeed=direction*3;
            }
            else if(stopTime<10 && stopTime>=8){
                runSpeed=direction*2;
            }
            else if(stopTime<8 && stopTime>0){
                runSpeed=direction*1;
            }
            moveX(runSpeed);
            
            
        }
        /*if(dir!=0){
                sound.Loop(walk);
            }
            else if(dir==0){
                sound.Stop(walk);
            }*/
        System.out.println(runSpeed);

        //flyttar gubben ut ur objekt han sprungit in i, genom att kolla från vilket
        //håll han kom in från, och sedan flytta ut honom igen
        for (int i=0;i<entities.size(); i++) {
                    if(collisionBox().intersects(entities.get(i).collisionBox())){
                        if((position_x-entities.get(i).position_x)>Game.square/2){
                            position_x=entities.get(i).position_x+entities.get(i).s.getWidth();
                        }
                        else if((entities.get(i).position_x-position_x)>Game.square/2){
                            position_x=entities.get(i).position_x-entities.get(i).s.getWidth();
                        }    
                    }

        }

    }
    public void jump(List<Entity> entities){

        if(collisionUnder(entities,0)==true){
            airSpeed=-25;
            moveY(-3);
            sound.Play(jump);
        }



    }

    

    public void gravity(List<Entity> entities){

        airSpeed=airSpeed+2;


        if(airSpeed>30){
            airSpeed=30;
        }
        if(airSpeed!=0){
        moveY(airSpeed);
        }

        //Flyttar ut gubben ur objekt han kan ha fastnat i
         for (int i=0;i<entities.size(); i++) {
            if(collisionBox().intersects (entities.get(i).collisionBox())){
                //Om gubben fastnar underifrån flyttas han ner
                if(entities.get(i).position_y<position_y){
                    position_y=entities.get(i).position_y+entities.get(i).s.getHeight();
                    airSpeed=0;
                }
                //Annars, då gubben fastnar uppifrån flyttas han upp
                else{
                    position_y=entities.get(i).position_y-entities.get(i).s.getHeight();
                    airSpeed=0;
                }
            }
         }
        
    }
    public boolean collisionUnder(List<Entity> entities,int pix){
        Rectangle temp = new Rectangle(position_x, position_y+s.getHeight()+1+pix, s.getWidth(), 1);


        for (int i=0;i<entities.size(); i++) {
            if(temp.intersects(entities.get(i).collisionBox())){
                airSpeed=0;
                return true;
            }
        }
        return false;
    }

    public boolean collisionOver(List<Entity> entities){
        Rectangle temp = new Rectangle(position_x, position_y-1, s.getWidth(), 1);

        for (int i=0;i<entities.size(); i++) {
            if(temp.intersects(entities.get(i).collisionBox())){
                airSpeed=0;
                return true;
            }

        }
        return false;
    }

    public boolean collisionLeft(List<Entity> entities,int pix){
        Rectangle temp = new Rectangle(position_x-1-pix, position_y, 1, s.getHeight());

        for (int i=0;i<entities.size(); i++) {
            if(temp.intersects(entities.get(i).collisionBox())){
                return true;
            }

        }
        return false;
    }

    public boolean collisionRight(List<Entity> entities,int pix){
        Rectangle temp = new Rectangle(position_x+s.getWidth()+1+pix, position_y, 1, s.getHeight());

        for (int i=0;i<entities.size(); i++) {
            if(temp.intersects(entities.get(i).collisionBox())){
                return true;
            }

        }
        return false;
    }





}
